package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UploadController {

    @Resource
    private AliyunOSSOperator ossOperator;
    //文件上传
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        if (!file.isEmpty() && file != null) {
            String url = ossOperator.upload(file.getBytes(), file.getOriginalFilename());
            return Result.success(url);
        }

//        byte[] bytes = Files.readAllBytes(Paths.get(file));
//        String url = ossOperator.upload(bytes, "1.png");
//        System.out.println("url = " + url);

        return Result.error("上传的文件不能为空！");
    }
}
